package com.ygqh.baby.service.impl;
import java.util.List;
import  com.ygqh.baby.ao.QueryInfo;
import  com.ygqh.baby.ao.ResultSet;
import  com.ygqh.baby.po.AdminPermission;
import  com.ygqh.baby.mapper.AdminPermissionMapper;
import  com.ygqh.baby.service.AdminPermissionService;
import  org.springframework.util.Assert;
import java.util.Arrays;
import  org.springframework.stereotype.Service;
import  org.springframework.beans.factory.annotation.Autowired;

import  com.ygqh.baby.po.AdminPermissionExample;
import  com.ygqh.baby.ao.DataStatus;

@Service
public class AdminPermissionServiceImpl implements AdminPermissionService{
	
	@Autowired
	private AdminPermissionMapper adminPermissionMapper;
	
	@Override
	public List<AdminPermission> find() {
		AdminPermissionExample example = new AdminPermissionExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return adminPermissionMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		AdminPermissionExample example = new AdminPermissionExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return adminPermissionMapper.countByExample(example);
	}

	@Override
	public AdminPermission findById(Long id) {
		return adminPermissionMapper.selectByPrimaryKey(id);
	}


	

	@Override
	public int save(AdminPermission adminPermission) {
		return adminPermissionMapper.insertSelective(adminPermission);
	}

	@Override
	public int update(AdminPermission adminPermission) {
		return adminPermissionMapper.updateByPrimaryKeySelective(adminPermission);
	}

	@Override
	public ResultSet<AdminPermission> search(QueryInfo queryInfo,String q) {
		List<AdminPermission>  list =  adminPermissionMapper.selectSuper(queryInfo, q);
		int count = adminPermissionMapper.countSuper(q);
		ResultSet<AdminPermission> resultSet = new ResultSet<AdminPermission>(count,list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		AdminPermissionExample example = new AdminPermissionExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		AdminPermission record = new AdminPermission();
		record.setStatus(DataStatus.Delete);

		return adminPermissionMapper.updateByExampleSelective(record, example);
	}
	
	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[]{id});
	}

	@Override
	public int saveOrUpdate(AdminPermission adminPermission) {
		int r = 0;
		if(adminPermission.getId() != null) {
			r = this.update(adminPermission);
		}else {
			r = this.save(adminPermission);
		}
		return r;
	}
	
	@Override
	public List<AdminPermission> findAllPermission(Long parentId) {
		AdminPermissionExample example = new AdminPermissionExample();
		example.createCriteria().andParentIdEqualTo(parentId).andStatusEqualTo(DataStatus.Valid.name());
		List<AdminPermission> permissions =  adminPermissionMapper.selectByExample(example);
		for(AdminPermission p:permissions) {
			p.setChildAdminPermissions(findAllPermission(p.getId()));
		}
		return permissions;
	}

}

